package com.health.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.health.domain.pojo.Checkitem;
import com.health.domain.query.PageQueryDTO;
import com.health.domain.vo.PageBean;
import com.health.domain.vo.Result;
import com.health.mapper.CheckgroupCheckitemMapper;
import com.health.mapper.CheckitemMapper;
import com.health.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 检查项业务实现
 * @author ghy
 * @version 1.0
 * @since 2025-06-21 17:05
 */
@Service
public class CheckitemServiceImpl implements CheckitemService {

    @Autowired
    private CheckitemMapper checkitemMapper;
    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;

    /**
     * 分页条件查询
     *
     * @param pageQueryDTO 分页参数
     * @return 分页结果
     */
    @Override
    public PageBean<Checkitem> findPage(PageQueryDTO pageQueryDTO) {
        // 1.设置分页参数
        Integer currentPage = pageQueryDTO.getCurrentPage();
        Integer pageSize = pageQueryDTO.getPageSize();
        String queryString = pageQueryDTO.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        // 2.执行查询
        List<Checkitem> checkitemList = checkitemMapper.selectList(queryString);
        // 3.封装数据
        PageInfo<Checkitem> pageInfo = new PageInfo<>(checkitemList);
        return new PageBean<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增
     *
     * @param checkitem 检查项
     * @return 统一结果
     */
    @Override
    public Result<String> add(Checkitem checkitem) {
        // 项目编码判重  select count(*) from t_checkitem where code = ?
        Integer count = checkitemMapper.selectByCode(checkitem.getCode());
        if(count > 0) {
            throw new RuntimeException("项目编码已存在，请更换！");
        }
        // 项目名称判重
        count = checkitemMapper.selectByName(checkitem.getName());
        if(count > 0) {
            throw new RuntimeException("项目名称已存在，请更换！");
        }
        checkitemMapper.insert(checkitem);
        return Result.success("新增检查项成功", null);
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Result<Checkitem> findById(Integer id) {
        Checkitem checkitem = checkitemMapper.selectById(id);
        return Result.success(checkitem);
    }

    /**
     * 修改
     *
     * @param checkitem
     * @return
     */
    @Override
    public Result<String> edit(Checkitem checkitem) {
        // 判重：除自己原先的值之外的值不能重复
        Checkitem dbCheckitem = checkitemMapper.selectById(checkitem.getId());
        // 如果页面传过来的项目编码值跟原先项目编码不一致，需要判重
        if(!StrUtil.equals(dbCheckitem.getCode(), checkitem.getCode())) {
            Integer count = checkitemMapper.selectByCode(checkitem.getCode());
            if(count > 0) {
                throw new RuntimeException("项目编码已存在，请更换！");
            }
        }
        if(!StrUtil.equals(dbCheckitem.getName(), checkitem.getName())) {
            Integer count = checkitemMapper.selectByName(checkitem.getName());
            if(count > 0) {
                throw new RuntimeException("项目名称已存在，请更换！");
            }
        }
        // 修改
        checkitemMapper.updateById(checkitem);
        return Result.success("编辑检查项成功", null);
    }

    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @Override
    public Result<String> deleteById(Integer id) {
        // 如果当前检查项被检查组关联了，则不能删除
        Integer count = checkgroupCheckitemMapper.countByCheckitem(id);
        if(count > 0) {
            throw new RuntimeException("当前检查项有关联的检查组，不能删除！");
        }
        checkitemMapper.deleteById(id);
        return Result.success("删除检查项成功", null);
    }

    @Override
    public Result<List<Checkitem>> findAll() {
        List<Checkitem> checkitemList = checkitemMapper.selectList(null);
        return Result.success(checkitemList);
    }

}